﻿import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Templates 2.12
import "../components"


/* 样本板显示与修改控制 */
Item {
    id: root
    readonly property real dp: config.dp

    property bool isEditable: true

    property string channelName1: "FAM"
    property string channelName2: "VIC"
    property string channelName3: "ROX"
    property string channelName4: "CY5"

    signal sampleChanged()

    // Public ====================================================================================

    onVisibleChanged: {
        edit_name.model = CExpInfo.selsamplename()
        edit_type.model = CExpInfo.selsampletype()
    }

    // 获取当前样本板信息
    function getSampleList(){
        return sampleList
    }

    // 设置当前样本板信息
    function setSampleList(slist){
        sampleList = slist
        cSamplePannel.setSampleList(sampleList)
    }

    // 初始化为空
    function initSampleList(){
        sampleList = []
        for(let r = 0; r < 8; r ++){
            for(let c = 0; c < 12; c ++){
                sampleList.push({
                                    "RowNum": r,
                                    "ColNum": c,
                                    "SampleName": "",
                                    "SampleType": "",
                                    "SampleConc": "",
                                    "FAM": 0,
                                    "ROX": 0,
                                    "VIC": 0,
                                    "CY5": 0
                                })
            }
        }
        cSamplePannel.setSampleList(sampleList)
        root.sampleChanged()
    }

    // 更新下拉框的候选项
    function updateComboList(sampleNameList, sampleTypeList){
        edit_name.model = sampleNameList
        edit_type.model = sampleTypeList
    }

    // 设置通道名称
    function setChannelName(list){
        channelName1 = list[0]
        channelName2 = list[1]
        channelName3 = list[2]
        channelName4 = list[3]
    }


    // Private ================================================================================
    BoxSampleList{
        id: boxSampleList
    }

    // 当前样本板显示的实验信息
    property var sampleInfo: ({
                                  "SampleName": "",
                                  "SampleType": "",
                                  "SampleConc": "",
                                  "FAM": 1,
                                  "VIC": 1,
                                  "ROX": 1,
                                  "CY5": 1
                              })
    property var sampleList: []
    property var countU: 0
    property var countS: 0
    property var countN: 0
    property var countAll: 0
    property var datainsampleList: []


    function clearSelect(){     // 清除选定区域的内容
        mutexInfo = true
        edit_name.changeCurrentIndex(-1)
        edit_type.changeCurrentIndex(-1)
        edit_conc.setText("")
        check_FAM.check = false
        check_ROX.check = false
        check_VIC.check = false
        check_CY5.check = false
        mutexInfo = false
        changeSampleInfo("SampleName")
        changeSampleInfo("SampleType")
        changeSampleInfo("SampleConc")
        changeSampleInfo("FAM")
        changeSampleInfo("VIC")
        changeSampleInfo("ROX")
        changeSampleInfo("CY5")

    }

    function sampledatain(alldata){     // 样本模板导入
        sampleList = []
        for(let r = 0; r < 8; r ++){
            for(let c = 0; c < 12; c ++){
                sampleList.push({
                                    "RowNum": r,
                                    "ColNum": c,
                                    "SampleName": alldata[r*12+c][0],
                                    "SampleType": alldata[r*12+c][1],
                                    "SampleConc": alldata[r*12+c][2],
                                    "FAM": (alldata[r*12+c][0]===""&&alldata[r*12+c][1]===""&&alldata[r*12+c][2]==="") ? 0:1,
                                    "ROX": (alldata[r*12+c][0]===""&&alldata[r*12+c][1]===""&&alldata[r*12+c][2]==="") ? 0:1,
                                    "VIC": (alldata[r*12+c][0]===""&&alldata[r*12+c][1]===""&&alldata[r*12+c][2]==="") ? 0:1,
                                    "CY5": (alldata[r*12+c][0]===""&&alldata[r*12+c][1]===""&&alldata[r*12+c][2]==="") ? 0:1
                                })
            }
        }
        root.sampleChanged()
        cSamplePannel.setSampleList(sampleList)
    }

    function changeSampleInfo(key){

        if(mutexInfo) {  return  }

        var state
        if(key==="SampleName") state = edit_name.getText()
        if(key==="SampleType") state = edit_type.getText()
        if(key==="SampleConc") state = Number(edit_conc.getText())
        if(key==="FAM") state = check_FAM.getStatus() ? 1 : 0
        if(key==="VIC") state = check_VIC.getStatus() ? 1 : 0
        if(key==="ROX") state = check_ROX.getStatus() ? 1 : 0
        if(key==="CY5") state = check_CY5.getStatus() ? 1 : 0
        var selectList = cSamplePannel.getSelectedList()
        if(key==="自增"){
            key = "SampleName"
            for(let i = 0; i < selectList.length; i ++){
                let idx = selectList[i]
                sampleList[idx][key] = "Sample"+(i+1)
            }
            cSamplePannel.setSampleList(sampleList)
        }else{
            for(let i = 0; i < selectList.length; i ++){
                let idx = selectList[i]
                sampleList[idx][key] = state
                if(key === "SampleType" && state !== "标准"){     // 非标准品时清空浓度
                    sampleList[idx]["SampleConc"] = ""
                }
            }
            cSamplePannel.setSampleList(sampleList)
        }
        if(selectList.length !== 0){        // 有改变内容时，发送样本被修改信号
            root.sampleChanged()
        }
    }


    property bool mutexInfo: false      // 用户选择操作时，右侧样本版信息变更不会触发状态变更信号

    // 样式 ===================================================================================
    anchors.fill: parent
    anchors.margins: 20*dp
    Item{              // 左侧样本板
        width: parent.width - 320*dp
        height: parent.height - 30*dp
        SamplePannel{
            id: cSamplePannel
            onSelectedChanged: {        // [TODO] 更新右侧样本信息
//                console.log("SelectChanged触发，长度为", selectedList.length)
                mutexInfo = true
                if(selectedList.length===1){
                    var idx = selectedList[0]
                    edit_name.changeCurrentText(sampleList[idx]["SampleName"])
                    edit_type.changeCurrentText(sampleList[idx]["SampleType"])
                    edit_conc.setText(sampleList[idx]["SampleConc"])
                    check_FAM.check = sampleList[idx]["FAM"] === 1
                    check_VIC.check = sampleList[idx]["VIC"] === 1
                    check_ROX.check = sampleList[idx]["ROX"] === 1
                    check_CY5.check = sampleList[idx]["CY5"] === 1

                }else{
                    edit_name.changeCurrentIndex(-1)
                    edit_type.changeCurrentIndex(-1)
                    edit_conc.setText("")
                    check_FAM.check = false
                    check_ROX.check = false
                    check_VIC.check = false
                    check_CY5.check = false
                }
                mutexInfo = false
            }
        }
    }
    Item{                       // 样本板下面的一小行
        height: 30*dp
        width: parent.width - 320*dp
        anchors.bottom: parent.bottom
        Text{
            text: "  未知:" + countU + "   标准:" + countS + "   阴性:" + countN + "   总数:" + countAll
            font.pixelSize: config.fontsize
        }

        PushButton{
            title: "清除"
            height: 30*dp
            width: 40*dp
            anchors.right: parent.right
            anchors.rightMargin: 182*dp
            text_color: "#1677FF"
            onReleased: {
                clearSelect()
            }
            visible: root.isEditable
        }PushButton{
            title: "全部清除"
            height: 30*dp
            width: 80*dp
            anchors.right: parent.right
            anchors.rightMargin: 90*dp
            text_color: "#1677FF"
            onReleased: {
                initSampleList()
            }
            visible: root.isEditable
        }PushButton{
            title: "列表展示"
            height: 30*dp
            width: 80*dp
            anchors.right: parent.right
            text_color: "#1677FF"
            onReleased: {
                boxSampleList.setData(sampleList)
                boxSampleList.show()
            }
        }
    }

    Rectangle{                   // 右侧样本信息
        width: 300*dp
        x: parent.width - 300*dp
        height: parent.height
        radius: 5*dp
        color: "#F7F7FA"
        Column{
            x: 20*dp
            y: 20*dp
            width: parent.width-40*dp
            spacing: 10*dp
            Text{
                text: "样本名称"
                font.pixelSize: config.fontsize
            }
            Row{
                width: parent.width
                spacing: 10*dp
                CustomComboBox{
                    id: edit_name
                    model: CExpInfo.selsamplename()
                    editWidth: parent.width/3*2
                    editable: root.isEditable
                    textEditable: true
                    onCurrentText: {
                        changeSampleInfo("SampleName")
                    }
                }PushButton{
                    title: "自增"
                    width: parent.width/3-10*dp
                    defaultMode2: true
                    isDisabled: !root.isEditable
                    onReleased: {
                        changeSampleInfo("自增")
                    }
                }
            }
            Text{
                text: "样本类型"
                font.pixelSize: config.fontsize
            }CustomComboBox{
                id: edit_type
                model: CExpInfo.selsampletype()
                editable: root.isEditable
                editWidth: parent.width
                onCurrentText: {
                    if(getText()==="标准"){
                    }else{
                        edit_conc.setText("")
                    }
                    changeSampleInfo("SampleType")
                }
            }
            Text{
                text: "浓度"
                font.pixelSize: config.fontsize
            }LineEditor{
                id: edit_conc
                editWidth: parent.width
                editable: root.isEditable && edit_type.getText()==="标准"
                onTextChanged: {
                    changeSampleInfo("SampleConc")
                }
            }

            Text{
                text: "通道"
                font.pixelSize: config.fontsize
            }Rectangle{
                width: parent.width
                height: 200 * dp
                border.color: "#AAAAAA"
                radius: 3 * dp
                Column{
                    x: 10*dp
                    y: 10*dp
                    id: buttonGroupItem

                    CustomCheckBox{
                        id: check_FAM
                        box_text: channelName1
                        fontColor: config.colorChannel1
                        checkable: root.isEditable
                        onCheckChanged: {
                            changeSampleInfo("FAM")
                        }
                    }CustomCheckBox{
                        id: check_VIC
                        box_text: channelName2
                        fontColor: config.colorChannel2
                        checkable: root.isEditable
                        onCheckChanged: {
                            changeSampleInfo("VIC")
                        }
                    }CustomCheckBox{
                        id: check_ROX
                        box_text: channelName3
                        fontColor: config.colorChannel3
                        checkable: root.isEditable
                        onCheckChanged: {
                            changeSampleInfo("ROX")
                        }
                    }CustomCheckBox{
                        id: check_CY5
                        box_text: channelName4
                        fontColor: config.colorChannel4
                        checkable: root.isEditable
                        onCheckChanged: {
                            changeSampleInfo("CY5")
                        }
                    }
                }
            }
            Item{
                height: 10*dp; width: 10*dp
            }PushButton{
                title: "样本信息导入"
                width: parent.width
                defaultMode2: true
                isDisabled: !root.isEditable
                onReleased: {
                    datainsampleList =CExpInfo.samplein()
                    if(datainsampleList[0]==="0"){
                        return
                    }else if(datainsampleList[0]==="1"){
                        mainwindow.showToast("文件格式错误", 1)
                        return
                    }else if(datainsampleList[0]==="2"){
                        return
                    }

                    sampledatain(datainsampleList)
                    mainwindow.showToast("导入成功", 0)
                }
            }
        }
    }

}

